package com.runvp.rest.app;

import com.runvp.common.annotation.Log;
import com.runvp.common.annotation.ParamValid;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.pay.OrderCreateModel;
import com.runvp.common.pay.adapay.build.BuildMainPay;
import com.runvp.common.util.OrderNoUtils;
import com.runvp.common.utils.ServletUtils;
import com.runvp.common.utils.validtion.CheckException;
import com.runvp.service.biz.domain.BizDevice;
import com.runvp.service.biz.service.IBizDeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 设备Controller
 *
 * @author runvp
 * @date 2022-09-18
 */
@Controller
@RequestMapping("/app/client/device")
public class DeviceController extends BaseController {

    @Autowired
    private IBizDeviceService bizDeviceService;

    /**
     * 设备心跳
     */
    @Log(title = "APP设备心跳", businessType = BusinessType.INSERT,isSaveRequestHeader = true,isOpenSysConfigSaveDb = true)
    @GetMapping("/xl")
    @ResponseBody
    public AjaxResult xl(String sn,String body) {
        CheckException.strEmptyThrowException(body,"心跳内容为空");
        if(StringUtils.isBlank(sn)){
            //从请求头获取
            sn = ServletUtils.getRequest().getHeader("devicesn");
        }
        return AjaxResult.success(bizDeviceService.xl(sn,body));
    }
    /**
     * 设备心跳
     */
    @ParamValid
    @Log(title = "APP设备心跳", businessType = BusinessType.INSERT)
    @PostMapping("/xl")
    @ResponseBody
    public AjaxResult deviceXL(@RequestBody BizDevice bizDevice) {
        return toAjax(bizDeviceService.deviceXL(bizDevice));
    }

    /**
     * APP创建订单
     */
    @ParamValid
    @Log(title = "APP创建订单", businessType = BusinessType.INSERT)
    @PostMapping("/app/create/order")
    @ResponseBody
    public AjaxResult createOrder(@RequestBody OrderCreateModel model) {
        model.setOrderNo(OrderNoUtils.getOrderNo());
        return AjaxResult.success(BuildMainPay.merchantPay(model));
    }
}
